package com.zros.modules.system.service.impl;

import com.zros.modules.system.dto.UpdateRoleDataResource;
import com.zros.modules.system.dto.UpdateRoleResource;
import com.zros.modules.system.entity.DataResource;
import com.zros.modules.system.entity.Resource;
import com.zros.modules.system.entity.RoleDataResource;
import com.zros.modules.system.entity.RoleResource;
import com.zros.modules.system.mapper.RoleDataResourceMapper;
import com.zros.modules.system.service.IDataResourceService;
import com.zros.modules.system.service.IResourceService;
import com.zros.modules.system.service.IRoleDataResourceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author pangyinqiang
 * @since 2019-04-12
 */
@Service
public class RoleDataResourceServiceImpl extends ServiceImpl<RoleDataResourceMapper, RoleDataResource> implements IRoleDataResourceService {

	@Autowired
	private IDataResourceService dataResourceService;
	
	
	//后续业务代码中，需要获取当前的所有数据权限
	//@Override
	/*
	 * @Cacheable(value = "data_resources", key = "'role_id_'.concat(#roleId)")
	 * public List<DataResource> queryDataResourceByRoleId(Long roleId) {
	 * QueryWrapper<RoleDataResource> ew = new QueryWrapper<RoleDataResource>();
	 * ew.eq("role_id", roleId); List<RoleDataResource> roleDataResourceList =
	 * this.list(ew); if (!CollectionUtils.isEmpty(roleDataResourceList)) {
	 * List<Long> dataResourceIds = new ArrayList<Long>(); for
	 * (RoleDataResource roleDataResource : roleDataResourceList) {
	 * dataResourceIds.add(roleDataResource.getDataResourceId()); }
	 * QueryWrapper<DataResource> ewResource = new QueryWrapper<DataResource>();
	 * ewResource.in("id", dataResourceIds); List<DataResource> dataResourceList =
	 * dataResourceService.list(ewResource); return dataResourceList; } else {
	 * return null; } }
	 */
	
	@Override
	//@CacheEvict(value = "resources", allEntries = true)
    public boolean updateList(UpdateRoleDataResource updateRoleDataResource) {
        List<RoleDataResource> addList = updateRoleDataResource.getAddDataResources();
        if (!CollectionUtils.isEmpty(addList)) {
            this.saveBatch(addList);
        }
        List<RoleDataResource> delList = updateRoleDataResource.getDelDataResources();
        if (!CollectionUtils.isEmpty(delList)) {
            List<Long> resIdList = new ArrayList<>();
            for (RoleDataResource rr : delList) {
                resIdList.add(rr.getDataResourceId());
            }
            Long roleId = updateRoleDataResource.getRoleId();
            QueryWrapper<RoleDataResource> ewResource = new QueryWrapper<RoleDataResource>();
            ewResource.eq("role_id", roleId).in("data_resource_id", resIdList);
            this.remove(ewResource);
        }
        return true;
    }
}
